import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import ApplyInvoice from '@/components/ApplyInvoice' // secondary package based on el-pagination
import SelfBuy from '@/components/selfBuy' // secondary package based on el-pagination
import {getOrderList,delOrder,cancelOrder} from "@/api/studyspace"
export default {
  name: "my-order",
  components: {
    Pagination,
    ApplyInvoice,
    SelfBuy,
  },
  data() {
    return {
      listQuery: {
        appId:"",
        pageNum: 1,
        pageSize: 10,
        orderName: "",
        orderSort: "",
        queryState:"0",
      },
      total: 0,
      list:[],
      dialogVisible:false,
      applyInvoiceOrder:{},
      showBuy:false,
      selfBuyOrderId:""
    }
  },
  methods: {
    isOrderDateThisYear(orderDate){
      const today = new Date();
      const orderYear = new Date(orderDate).getFullYear();
      return orderYear === today.getFullYear();
    },
    handleClick(tab, event) {
      this.getList();
    },
    getList() {
      getOrderList(this.listQuery).then((response) =>{
        this.list = response.data.list;
        this.total = Number(response.data.total)
      }).catch((err) =>{
        this.$message.error(err.message || 'error')
      });
    },
    toOrderDetail(order){
      this.$router.push({
        path:"/studyspace/my-order-detail",
        query:{
          orderId:order.id,
          productType:order.productType
        }
      })
    },
    toOrderBuyDetail(order){
      this.selfBuyOrderId = order.id;
      this.showBuy = true;
    },
    closeShowBuy(flag){
      this.showBuy = flag;
    },
    toPay(order){
      if ( order.productType == 0 ) {
        this.$router.push({
          path:"/pay/toPayCourse",
          query:{
            type:order.payType,
            orderId:order.id,
            orderNo:order.orderNo,
          }
        })
      }else if (order.productType == 1) {
        this.$router.push({
         path:"/pay/toPay",
         query:{
           type:order.payType,
           orderId:order.id,
           orderNo:order.orderNo,
           trainingId:order.productId
         }
       })
      }
      else if (order.productType == 3) {
        this.$router.push({
          path:"/pay/toPay",
          query:{
            type:order.payType,
            orderId:order.id,
            orderNo:order.orderNo,
          }
        })

      }
    },
    applyInvoice(order){
      this.applyInvoiceOrder = order;
      this.dialogVisible =true;

    },
    downInvoice(obj){
      var link = document.createElement('a');
      link.target = "_blank";
      link.href = obj.invoiceUrl;
      link.click();
    },
    del(orderId){
      this.$confirm('您是否确定删除该订单？删除后将无法恢复', '提示', {
        confirmButtonText: '确定',
        type: 'warning'
      }).then(() =>{
        delOrder(orderId).then((response) =>{
          this.$message.success("删除成功");
          this.getList();
        }).catch((err) =>{
          this.$message.error(err.message || 'error')
        });
      })
    },
    removeOrder(orderId){
      this.$confirm('您是否确定取消该订单？取消后将无法恢复', '提示', {
        confirmButtonText: '确定',
        type: 'warning'
      }).then(() =>{
        cancelOrder(orderId).then((response) =>{
          this.$message.success("取消成功");
          this.getList();
        }).catch((err) =>{
          this.$message.error(err.message || 'error')
        });
      })
    },
    closeApply(){
      this.dialogVisible = false;
      this.getList()
    }
  },
  mounted(){
    this.getList();
  }
}
